package se.topshelf.golftracker.web.controller.country;


import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.validation.Validator;
import org.springframework.web.bind.WebDataBinder;
import org.springframework.web.bind.annotation.*;
import se.topshelf.golftracker.core.domain.Continent;
import se.topshelf.golftracker.core.domain.Country;
import se.topshelf.golftracker.core.service.BaseService;
import se.topshelf.golftracker.core.service.PlayerService;
import se.topshelf.golftracker.web.controller.BaseEntityController;
import se.topshelf.golftracker.web.editor.BaseEntityEditor;

import javax.annotation.Resource;

@Controller
@SessionAttributes("entity")
@RequestMapping("/country")
public class CountryController extends BaseEntityController<Country>{

    @Resource  // Automatically set ("wired") by Spring
    private PlayerService playerService;
    private static final String ALL_CONTINENTS_MODEL_ATTRIBUTE = "allContinents";

    public CountryController() {
        super(Country.class);
    }

    @Override
    protected String getEntityUrlContextPath() {
        return "country";
    }

    @Override
    protected BaseService getBaseService() {
        return playerService;
    }

    @Override
    protected void addModelReferenceData(ModelMap model, Country entity) {
        model.put(ALL_CONTINENTS_MODEL_ATTRIBUTE, fetchAllEntitiesAndBuildReferenceDataFor(Continent.class));

    }

    @InitBinder
    protected void initBinder(WebDataBinder binder) {
        binder.registerCustomEditor(Continent.class, new BaseEntityEditor<Continent>(Continent.class, playerService));
    }

    @Override
    protected Validator getValidator() {
        return new CountryValidator(playerService);
    }
}
